#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/stat.h>
#define BUFFER 128 
#define PERM S_IRUSR|S_IWUSR

typedef struct _stMsg
{
  long lType;
  char szBuf[BUFFER + 1];
}TMsg;
int main(int argc ,char* argv[])
{
  int msqid = msgget(10000, PERM | IPC_CREAT);

  printf("sizeof TMsg = [%d]\n", sizeof(TMsg));

  TMsg t_msg;
  memset(&t_msg, 0x00, sizeof(TMsg));
  t_msg.lType = 1;

  snprintf(t_msg.szBuf, BUFFER, "%s", "he");

  msgsnd(msqid, &t_msg, sizeof(TMsg), 0);
  
#if 0
  TMsg t_recv
  memset(&t_recv, 0x00, sizeof(TMsg));
  msgrcv(msqid, &t_recv, sizeof(TMsg), 1, 0);
  printf("recv:[%s]\n", pt_recv->szBuf);

#else
  char buf[1024] = {0};
  msgrcv(msqid, buf, sizeof(TMsg), 1, 0);

  /**
   * There we can find that in the buf, after postion 136
   * has something no correct, and if we use TMsg directly
   * the something will destroy the stack,the the program will crash
   */

  TMsg *pt_recv = (TMsg*)buf;
  printf("recv:type [%ld] buf = [%s]\n",
         pt_recv->lType,
         pt_recv->szBuf);
#endif
  return 0;
}
